﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace NS
{

    [HotUpdate]
    [Description("付款单保存时回写付款方式到采购订单付款计划")]
    public class UpdatePayment : AbstractOperationServicePlugIn
    {
        public override void OnPreparePropertys(Kingdee.BOS.Core.DynamicForm.PlugIn.Args.PreparePropertysEventArgs e)
        {
            e.FieldKeys.Add("FID");
            base.OnPreparePropertys(e);
        }

        public override void AfterExecuteOperationTransaction(AfterExecuteOperationTransaction e)
        {
            base.AfterExecuteOperationTransaction(e);
            string FID = "";
            foreach (DynamicObject item in e.DataEntitys)
            {
                if (item["ID"] != null && Convert.ToString(item["ID"]) != "")
                {
                    FID = Convert.ToString(item["ID"]);
                }
                string SQL = "/*dialect*/" + string.Format(@"UPDATE T SET T.F_WNEG_Assistant7=T5.F_WNEG_ASSISTANT FROM T_PUR_POORDERINSTALLMENT T
                LEFT JOIN T_CN_PAYAPPLYENTRY_LK T1 ON T.FID=T1.FSBILLID AND T.FENTRYID=T1.FSID
                LEFT JOIN T_CN_PAYAPPLYENTRY T2 ON T1 .FENTRYID=T2.FENTRYID
                LEFT JOIN T_AP_PAYBILLSRCENTRY_LK T3 ON T2.FID=T3.FSBILLID AND T2.FENTRYID=T3.FSID
                LEFT JOIN T_AP_PAYBILLSRCENTRY T4 ON T3.FENTRYID=T4.FENTRYID
				LEFT JOIN T_AP_PAYBILLENTRY T5 ON T4.FID=T5.FID
                WHERE T5.F_WNEG_ASSISTANT<>'' AND T4.FID='{0}'", FID);
                DBUtils.ExecuteDynamicObject(this.Context, SQL);
            }
        }
    }
}
